﻿using CCWin;
using SuperMap.Analyst.NetworkAnalyst;
using SuperMap.Data;
using SuperMap.Data.Topology;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 构建网络数据集
    /// </summary>
    public partial class TPWSJJ : CCSkinMain
    {
        private Datasource m_datasource;
        /// <summary>
        /// 构建网络数据集窗体
        /// </summary>
        public TPWSJJ()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 构建网络数据集窗体
        /// </summary>
        public TPWSJJ(Datasource datasource)
        {
            InitializeComponent();
            m_datasource = datasource;
            Fill_DataSetGridView(datasource);
            this.DaduanRongXian_TextBox.Text = "0.00001";
            this.DialogResult = DialogResult.No;
        }
        /// <summary>
        /// 填充DataSetGridView
        /// </summary>
        /// <param name="m_datasource"></param>
        private void Fill_DataSetGridView(Datasource m_datasource)
        {
            int i = 0;
            foreach (DatasetVector datasetVector in m_datasource.Datasets)
            {
                DataSetGridView.Rows.Add();
                DataSetGridView.Rows[i].Cells[0].Value = datasetVector.Name;
                DataSetGridView.Rows[i++].Cells[1].Value = datasetVector.Type;
            }
        }
        /// <summary>
        /// 绑定点线和线线打断设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XianXianDaduan_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.XianXianDaduan_CheckBox.Checked)
            {
                this.DianXianDaduan_CheckBox.Checked = true;
                this.DianXianDaduan_CheckBox.Enabled = false;
            }
            else
            {
                this.DianXianDaduan_CheckBox.Enabled = true;
            }
        }
        /// <summary>
        /// 获取用户设置的参数
        /// </summary>
        public string[] LineNames;//线数据名称集合
        public string[] PointNames;//点数据名称集合
        public double RongXian;//打断容限
        public string SaveName;//保存文件名
        public NetworkSplitMode NetworkSplitMode = NetworkSplitMode.None;//打断模式
        public string[] LineFielinfos;//获取线数据非系统字段
        public string[] PointFielinfos;//获取点数据非系统字段
        private bool GetCanShuSheZhi_Fun()
        {
            if (DataSetGridView.SelectedRows.Count == 0) { MessageBox.Show("请选择至少一个线数据集!"); return false; }
            else
            {
                //获取线点数据集名称
                LineNames = new string[DataSetGridView.SelectedRows.Count]; int i = 0; int x = 0;
                PointNames = new string[DataSetGridView.SelectedRows.Count]; int n = 0; int y = 0;
                //获取点线数据集非系统字段
                foreach (DataGridViewRow row in DataSetGridView.SelectedRows)
                {
                    if (row.Cells[1].Value.ToString() == "Line")
                    {
                        LineNames[i] = row.Cells[0].Value.ToString();
                        DatasetVector datasetVector = m_datasource.Datasets[LineNames[i++]] as DatasetVector;
                        FieldInfos fieldInfos = datasetVector.FieldInfos;
                        LineFielinfos = new string[fieldInfos.Count * DataSetGridView.SelectedRows.Count];//获取每个线数据集的非系统字段
                        for (int m = 0; m < fieldInfos.Count; m++)
                        {
                            if (!fieldInfos[m].IsSystemField)
                            {
                                LineFielinfos[x++] = fieldInfos[m].Caption;
                            }
                        }
                        LineFielinfos = LineFielinfos.Where(p => !string.IsNullOrEmpty(p)).ToArray();//清除空值
                    }
                    if (row.Cells[1].Value.ToString() == "Point")
                    {
                        PointNames[n] = row.Cells[0].Value.ToString();
                        DatasetVector datasetVector = m_datasource.Datasets[PointNames[n++]] as DatasetVector;
                        FieldInfos fieldInfos = datasetVector.FieldInfos;
                        PointFielinfos = new string[fieldInfos.Count * DataSetGridView.SelectedRows.Count]; //获取每个点数据集的非系统字段
                        for (int m = 0; m < fieldInfos.Count; m++)
                        {
                            if (!fieldInfos[m].IsSystemField)
                            {
                                PointFielinfos[y++] = fieldInfos[m].Caption;
                            }
                        }
                        PointFielinfos = PointFielinfos.Where(p => !string.IsNullOrEmpty(p)).ToArray();//清除空值
                    }
                }
                LineNames = LineNames.Where(p => !string.IsNullOrEmpty(p)).ToArray();//清除空值
                PointNames = PointNames.Where(p => !string.IsNullOrEmpty(p)).ToArray();//清除空值
                if (i == 0) { MessageBox.Show("请选择至少一个线数据集!"); return false; }
                else
                {
                    //获取打断容限
                    if (DaduanRongXian_TextBox.Text != "")
                    {
                        RongXian = double.Parse(DaduanRongXian_TextBox.Text);
                        //获取保存文件名
                        if (ToSaveSet_textBox.Text != "")
                        {
                            bool bool_name = true;
                            foreach (DataGridViewRow row in DataSetGridView.Rows)
                            {
                                if (ToSaveSet_textBox.Text == row.Cells[0].Value.ToString())
                                {
                                    MessageBox.Show("保存文件名重复！");
                                    bool_name = false;
                                    return false;
                                }
                            }
                            if (bool_name)
                            {
                                SaveName = "Network_" + ToSaveSet_textBox.Text;
                                //获取打断设置
                                if (!DianXianDaduan_CheckBox.Checked && !XianXianDaduan_CheckBox.Checked) { NetworkSplitMode = NetworkSplitMode.None; }
                                if (DianXianDaduan_CheckBox.Checked && !XianXianDaduan_CheckBox.Checked) { NetworkSplitMode = NetworkSplitMode.LineSplitByPoint; }
                                if (XianXianDaduan_CheckBox.Checked) { NetworkSplitMode = NetworkSplitMode.LineSplitByPointAndLine; }
                                return true;
                            }
                        }
                        else
                        {
                            MessageBox.Show("保存文件名为空");
                            return false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("容限值为空");
                        return false;
                    }
                }
                return true;
            }
        }
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Create_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (GetCanShuSheZhi_Fun())
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void No_Button_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.No;
                this.Dispose();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
